package com.ecommerce.product.controller;

import com.ecommerce.product.dto.ProductCreateRequest;
import com.ecommerce.product.dto.ProductPageResponse;
import com.ecommerce.product.dto.ProductResponse;
import com.ecommerce.product.dto.ProductUpdateRequest;
import com.ecommerce.product.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 产品控制器
 */
@RestController
@RequestMapping("/products")
@RequiredArgsConstructor
@Tag(name = "产品管理", description = "产品相关接口")
public class ProductController {

    private final ProductService productService;

    /**
     * 创建产品
     * @param createRequest 创建请求
     * @return 产品响应
     */
    @PostMapping
    @Operation(summary = "创建产品", description = "创建一个新的产品")
    public ResponseEntity<ProductResponse> createProduct(
            @Valid @RequestBody ProductCreateRequest createRequest) {
        ProductResponse response = productService.createProduct(createRequest);
        return ResponseEntity.ok(response);
    }

    /**
     * 更新产品
     * @param productId 产品ID
     * @param updateRequest 更新请求
     * @return 产品响应
     */
    @PutMapping("/{productId}")
    @Operation(summary = "更新产品", description = "更新指定产品信息")
    public ResponseEntity<ProductResponse> updateProduct(
            @Parameter(description = "产品ID", required = true)
            @PathVariable Long productId,
            @Valid @RequestBody ProductUpdateRequest updateRequest) {
        ProductResponse response = productService.updateProduct(productId, updateRequest);
        return ResponseEntity.ok(response);
    }

    /**
     * 获取产品详情
     * @param productId 产品ID
     * @return 产品响应
     */
    @GetMapping("/{productId}")
    @Operation(summary = "获取产品详情", description = "根据产品ID获取产品详细信息")
    public ResponseEntity<ProductResponse> getProductById(
            @Parameter(description = "产品ID", required = true)
            @PathVariable Long productId) {
        ProductResponse response = productService.getProductById(productId);
        return ResponseEntity.ok(response);
    }

    /**
     * 分页获取产品列表
     * @param page 页码
     * @param size 每页大小
     * @return 产品分页响应
     */
    @GetMapping
    @Operation(summary = "获取产品列表", description = "分页获取产品列表")
    public ResponseEntity<ProductPageResponse> getProducts(
            @Parameter(description = "页码", example = "1")
            @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "10")
            @RequestParam(defaultValue = "10") Integer size) {
        ProductPageResponse response = productService.getProducts(page, size);
        return ResponseEntity.ok(response);
    }

    /**
     * 删除产品
     * @param productId 产品ID
     * @return 操作结果
     */
    @DeleteMapping("/{productId}")
    @Operation(summary = "删除产品", description = "删除指定产品")
    public ResponseEntity<Void> deleteProduct(
            @Parameter(description = "产品ID", required = true)
            @PathVariable Long productId) {
        productService.deleteProduct(productId);
        return ResponseEntity.ok().build();
    }

    /**
     * 减少库存
     * @param productId 产品ID
     * @param quantity 数量
     * @return 操作结果
     */
    @PostMapping("/{productId}/reduce-stock")
    @Operation(summary = "减少库存", description = "减少指定产品的库存")
    public ResponseEntity<Void> reduceStock(
            @Parameter(description = "产品ID", required = true)
            @PathVariable Long productId,
            @Parameter(description = "减少数量", required = true)
            @RequestParam Integer quantity) {
        productService.reduceStock(productId, quantity);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取热门商品
     * @param limit 限制数量
     * @return 热门商品列表
     */
    @GetMapping("/hot")
    @Operation(summary = "获取热门商品", description = "获取销量最高的热门商品")
    public ResponseEntity<List<ProductResponse>> getHotProducts(
            @Parameter(description = "限制数量", example = "8")
            @RequestParam(defaultValue = "8") Integer limit) {
        List<ProductResponse> hotProducts = productService.getHotProducts(limit);
        return ResponseEntity.ok(hotProducts);
    }

    /**
     * 搜索产品
     * @param keyword 搜索关键词
     * @param page 页码
     * @param size 每页大小
     * @return 产品分页响应
     */
    @GetMapping("/search")
    @Operation(summary = "搜索产品", description = "根据关键词搜索产品")
    public ResponseEntity<ProductPageResponse> searchProducts(
            @Parameter(description = "搜索关键词")
            @RequestParam(required = false) String keyword,
            @Parameter(description = "页码", example = "1")
            @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "10")
            @RequestParam(defaultValue = "10") Integer size) {
        ProductPageResponse response = productService.searchProducts(keyword, page, size);
        return ResponseEntity.ok(response);
    }
}